qui(
    function (require, exports, module) {
        //解说 部分
        Director.Commentary = (function () {
            var maxLength = 8;
            var pageBean = new Director.Util.createPageBean();

            function getCommentaryList(needTips) {
                var entity = Director.Util.getEntity();
                Director.Dao.getCommentaryList({directorId: entity.id, programId: entity.programQipuId}, function (data) {
                    var container = $("#commentary_area");
                    if (data != null && data.code == "A00000" && data.data.length > 0) {
                        makeCommentaryList(data.data);
                        container.show();
                        if (needTips) {
                            EpgWeb.Plugin.showTips({content: "获取解说源成功", type: "success"});
                        }
                    } else {
                        container.hide();
                        if (needTips) {
                            EpgWeb.Plugin.showTips({content: "未发现可用的解说源", type: "error"});
                        }
                    }
                })
            }

            function makeCommentaryList(data) {
                var container = $("#commentary_area");
                var content = '';
                var htmlArr = [];
                pageBean.setNewData(data,maxLength);
                var pageObj = pageBean.getCur();
                $.each(pageObj.rows, function (index, e) {
                    htmlArr.push(makeUpCommentaryDiv(e));
                });

                content = htmlArr.join('');
                container.html(content);
                //if( data.length > maxLength){
                //    SystemDialog.queryAlert("解说数量超过"+maxLength+"个");
                //}
            }

            function clearCommentaryList() {
                var container = $("#commentary_area");
                container.html('');
                container.hide();
            }

            function makeUpCommentaryDiv(item) {
                var options = encodeURIComponent(JSON.stringify(item));
                var htmlArr = [];

                if ($.inArray(item.status, [0, 1, 2]) >= 0) {
                    htmlArr.push('<div class="commentary_div" data-div-id="' + item.id + '" data=' + options + '"><div>');
                    htmlArr.push('<object type="application/x-shockwave-flash" data="/epg/swfs/SignalSound.swf" width="280px" height="100%">');
                    htmlArr.push('<param name="scale" value="noscale"><param name="salign" value="tl">');
                    htmlArr.push('<param name="wmode" value="transparent"><param name="swliveconnect" value="true">');
                    htmlArr.push('<param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always">');
                    htmlArr.push('<param name="allownetworking" value="all"><param name="bgcolor" value="#000000">');
                    htmlArr.push('<param name="flashvars" value="resourceId=' + item.id + '&amp;status=' + item.status + '&amp;volumn=' + item.volumn + '&amp;rtmpUrl=' + item.streamUrl + '">');
                    htmlArr.push('</object></div>');
                    htmlArr.push('<h2 style="text-align: center;font-size:150%;">' + item.name + '</h2></div>');
                }

                return htmlArr.join("");

            }

            function refreshCommentaryList(needTips) {
                getCommentaryList(needTips);
            }


            function delCommentary(data) {
                var entity = Director.Util.getEntity();
                var eid = data['resourceId'];
                var sendData = {id: eid, directorId: entity.id};
                SystemDialog.queryConfirm("确定要删除么？", function (flag) {
                    if (flag == true) {
                        Director.Dao.deleteCommentary(sendData, function (json) {
                            if (json != null && json.code == "A00000") {
                                EpgWeb.Plugin.showTips({content: "删除成功"});
                                Director.Commentary.refreshCommentaryList(false);
                            } else {
                                EpgWeb.Plugin.showTips({content: "删除失败 " + JSON.stringify(json), type: "error"});
                            }
                        })
                    }
                });
            }

            function playCommentary(data) {
                var entity = Director.Util.getEntity();
                var eid = data['resourceId'];
                var volumn = data['volumn'];

                var options = {
                    eid: eid,
                    did: entity.id,
                    pid: entity.programQipuId,
                    volumn: volumn
                };

                notifyChangeVolumn(options, function (options) {
                    notifyPlayCommentary(options, function (options) {
                        Director.Dao.editCommentary(
                            {id: options.eid, programId: options.pid, directorId: options.did, status: "1", volumn: options.volumn}, function (json) {
                                if (json != null && json.code == 'A00000') {
                                    EpgWeb.Plugin.showTips({content: "播出成功", type: "success"});
                                    //Director.Commentary.refreshCommentaryList(false);
                                } else {
                                    EpgWeb.Plugin.showTips({content: "播出失败 " + JSON.stringify(json), type: "error"});
                                }
                            })
                    })
                });
            }

            function notifyPlayCommentary(options, callback) {
                var handler = function () {
                    callback(options);
                }
                var request = {};

                request.type = 'get';
                request.url = Director.Config.EFFECT_BACNEND_URL_PREFIX + 'startcommentary';
                request.params = {
                    ID: options.eid,
                    PID: options.pid
                };

                request.directorId = options.did;
                request.logType = "118";

                request = JSON.stringify(request);
                Director.Dao.redirectApi({request: request}, function (json) {
                    if (json.error_code == 'A00000') {
                        handler();
                    } else {
                        EpgWeb.Plugin.showTips({content: "播出失败 " + JSON.stringify(json), type: "error"});
                    }
                    //self.callback(options);
                })
            }

            function stopCommentary(data) {
                var entity = Director.Util.getEntity();
                var eid = data['resourceId'];

                var options = {
                    eid: eid,
                    pid: entity.programQipuId,
                    did: entity.id
                };

                notifyStopCommentary(options, function (options) {
                    Director.Dao.editCommentary({id: options.eid, programId: options.pid, directorId: options.did, status: "0"}, function (json) {
                        if (json != null && json.code == 'A00000') {
                            EpgWeb.Plugin.showTips({content: "停止播出成功", type: "success"});
                            //Director.Commentary.refreshCommentaryList(false);
                        } else {
                            EpgWeb.Plugin.showTips({content: "停止播出失败 " + JSON.stringify(json), type: "error"});
                        }
                    })
                })
            }

            function notifyStopCommentary(options, callback) {
                var handler = function () {
                    callback(options);
                }
                var request = {};

                request.type = 'get';
                request.url = Director.Config.EFFECT_BACNEND_URL_PREFIX + 'stopcommentary';
                request.params = {
                    ID: options.eid,
                    PID: options.pid
                };
                request.directorId = options.did;
                request.logType = "119";

                request = JSON.stringify(request);
                Director.Dao.redirectApi({request: request}, function (json) {
                    if (json.error_code == 'A00000') {
                        handler();
                    } else {
                        EpgWeb.Plugin.showTips({content: "停止播出失败 " + JSON.stringify(json), type: "error"});
                    }
                    //self.callback(options);
                })
            }

            function notifyChangeVolumn(options, callback) {
                var handler = function () {
                    callback(options);
                }
                var request = {};

                request.type = 'get';
                request.url = Director.Config.EFFECT_BACNEND_URL_PREFIX + 'setcommentaryvolume';
                request.params = {
                    ID: options.eid,
                    PID: options.pid,
                    Volume: options.volumn
                };

                request.directorId = options.did;
                request.logType = "120";

                request = JSON.stringify(request);
                Director.Dao.redirectApi({request: request}, function (json) {
                    if (json.error_code == 'A00000') {
                        handler();
                    } else {
                        EpgWeb.Plugin.showTips({content: "停止播出失败 " + JSON.stringify(json), type: "error"});
                    }
                    //self.callback(options);
                })
            }

            function changeCommentaryVolumn(data) {
                var entity = Director.Util.getEntity();
                var eid = data['resourceId'];
                var volumn = data['volumn'];

                var options = {
                    eid: eid,
                    pid: entity.programQipuId,
                    did: entity.id,
                    volumn: volumn
                };

                notifyChangeVolumn(options, function (options) {
                    Director.Dao.editCommentary({id: options.eid, programId: options.pid, directorId: options.did, volumn: options.volumn}, function (json) {
                        if (json != null && json.code == 'A00000') {
                            EpgWeb.Plugin.showTips({content: "设置音量成功", type: "success"});
                            //self.refreshCommentaryList(false);
                        } else {
                            EpgWeb.Plugin.showTips({content: "设置音量失败 " + JSON.stringify(json), type: "error"});
                        }
                    })
                })
            }

            function checkCommentaryAfterEditDirector() {
                var container = $("#commentary_area");
                if(container.html() != "") {
                    EpgWeb.Plugin.showTips({content: "配置已修改，请重新获取解说源。", type: "success"})
                }

            }

            return {
                getCommentaryList: getCommentaryList,
                makeCommentaryList: makeCommentaryList,
                makeUpCommentaryDiv: makeUpCommentaryDiv,
                refreshCommentaryList: refreshCommentaryList,
                delCommentary: delCommentary,
                changeCommentaryVolumn: changeCommentaryVolumn,
                stopCommentary: stopCommentary,
                playCommentary: playCommentary,
                clearCommentaryList: clearCommentaryList,
                checkCommentaryAfterEditDirector: checkCommentaryAfterEditDirector,
            }
        })()
    }
);